//
//  AppDelegate.m
//  groupProject
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

#import "AppDelegate.h"
#import "Config.h"
#import "HomeViewController.h"
#import "CategoryViewController.h"
#import "ShopCartViewController.h"
#import "InfoViewController.h"

@interface AppDelegate ()<UITabBarControllerDelegate>

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    
    HomeViewController *home = [[HomeViewController alloc] init];
    UINavigationController *nav1 = [[UINavigationController alloc] initWithRootViewController:home];
    
    
    
    //second tab
    CategoryViewController *category = [[CategoryViewController alloc] init];
    UINavigationController *nav2 = [[UINavigationController alloc] initWithRootViewController:category];
    
    //third tab
    ShopCartViewController *shopcart = [[ShopCartViewController alloc] init];
    UINavigationController *nav3 = [[UINavigationController alloc] initWithRootViewController:shopcart];
    
    //fourth tab
    InfoViewController *information = [[InfoViewController alloc] init];
    UINavigationController *nav4 = [[UINavigationController alloc] initWithRootViewController:information];
    
    //set tabbar
    UITabBarController *tabbar = [[UITabBarController alloc] init];
    tabbar.viewControllers =  [NSArray arrayWithObjects:nav1, nav2, nav3, nav4, nil];
    
    //set singal viewcontroll of tabbar
    home.view.backgroundColor = [UIColor whiteColor];
    [self setTabbarViewController:home itemImage:@"tabbar_home_003" andSelectedImage:@"tabbar_selected_home_001" WithTitile:@"home"];
    //home.title = @"主页";
    
    category.view.backgroundColor = [UIColor yellowColor];
    [self setTabbarViewController:category itemImage:@"category_1" andSelectedImage:@"category_seclected_1" WithTitile:@"category"];
    category.title = @"分类";
    
    shopcart.view.backgroundColor = [UIColor purpleColor];
    [self setTabbarViewController:shopcart itemImage:@"shopcar_1" andSelectedImage:@"shopcar_seclected_1" WithTitile:@"购物车"];
    shopcart.title = @"购物车";
    
    information.view.backgroundColor = [UIColor blueColor];
    [self setTabbarViewController:information itemImage:@"person_1" andSelectedImage:@"person_seclected_1" WithTitile:@"我的"];
    //information.title = @"个人信息";
    
    
    self.window.rootViewController = tabbar;
    tabbar.delegate =self;
    
    //设定Tabbar的点击后的颜色
    
    [[UITabBar appearance] setTintColor:[UIColor redColor]];
   // [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor blackColor], NSForegroundColorAttributeName, nil] forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor redColor], NSForegroundColorAttributeName, nil] forState:UIControlStateSelected];
   // [[UITabBar appearance] setBackgroundColor:[UIColor whiteColor]];
    //设定Tabbar的颜色
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 48)];
    backView.backgroundColor = [UIColor whiteColor];
    [tabbar.tabBar insertSubview:backView atIndex:0];
    //[[UITabBar appearance] setBarTintColor:[UIColor clearColor]];
    
    return YES;
}

//设置 tabbar的视图组 中 各个视图对应的 item的图标；
- (void) setTabbarViewController:(UIViewController *)vc itemImage:(NSString *) imagename andSelectedImage:(NSString *) selectedimagename WithTitile:(NSString *) title {
    
    UIImage *unimage = [UIImage imageNamed:imagename];
    unimage = [unimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    UIImage *selimage = [UIImage imageNamed:selectedimagename];
    selimage = [selimage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:title image:unimage selectedImage:selimage];
}


- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 3) {
        UIViewController *selected = [tabBarController selectedViewController];
        if ([selected isEqual:viewController]) {
            return NO;
        }
    }
    return YES;
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
